package cc.angis.xiugallery;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cc.angis.xiugallery.common.GlobalConst;
import cc.angis.xiugallery.db.FavouriteDBHelper;
import cc.angis.xiugallery.db.TableConstants;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

public class AddFavouriteFragment extends SherlockFragment{
    private static final String TAG = "AddFavouriteFragment";
    
//    InputMethodManager imm;
    
    /**
     * Empty constructor as per the Fragment documentation
     */
    public AddFavouriteFragment() {}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        final View v = inflater.inflate(R.layout.addfavourite, container, false);
        final EditText editText = (EditText) v.findViewById(R.id.addfavourite_edit);
        editText.requestFocus();
//        imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
//		imm.showSoftInput(editText, InputMethodManager.RESULT_SHOWN);
//		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
        final Button button = (Button) v.findViewById(R.id.addfavourite_button);
        button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (editText.getText() != null){
					String name = editText.getText().toString();
					FavouriteDBHelper dbHelper = new FavouriteDBHelper(getActivity(), TableConstants.TABLE_FAVOURITE);
					try{
						if (dbHelper.isExist(TableConstants.FavouriteColumn.NAME, name))
							Toast.makeText(getActivity(), String.format(getString(R.string.toast_Fav_exist), name), Toast.LENGTH_SHORT).show();
						else {
							long id = dbHelper.insert(name);
							Intent i = getActivity().getIntent();
							i.putExtra(GlobalConst.INTENT_FAVOURITE_ID, (int)id);
							i.putExtra(GlobalConst.INTENT_FAVOURITE_NAME, name);
							getActivity().setResult(getActivity().RESULT_OK, i);
							getActivity().finish();
						}
					} catch(Exception e){
						
					} finally{
						dbHelper.closeDB();
					}
					
					
				}
					
				
			}
		});
        return v;
    }
    
	
	@Override
	public boolean onOptionsItemSelected(
			com.actionbarsherlock.view.MenuItem item) {
		switch(item.getItemId()){
			case android.R.id.home:
				if (getActivity() instanceof SlidingFragmentActivity){
					((SlidingFragmentActivity)getActivity()).getSlidingMenu().showBehind();
				} else
					getActivity().finish();
				break;
			default:
				break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onDestroyView() {
//		imm.hideSoftInputFromInputMethod(getActivity().getCurrentFocus().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
		super.onDestroyView();
	}
	

}

